package com.aluvfy.jdbc;

import com.aluvfy.jdbc.utils.DbUtils;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;

/*PreparedStatement-blob数据的插入和读取*/
public class JDBCTest15 {
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement ps = null;
        FileInputStream in = null;
        try {
            conn = DbUtils.getConnection();
            String sql = "insert into t_img(name, img) values(?, ?)";
            ps = conn.prepareStatement(sql);
            ps.setString(1, "img1");
            // 输入流  从硬盘中读取到java程序中
            in = new FileInputStream("D:\\C_Photo\\BIZ\\1642751539370164275153973.jpg");
            ps.setBlob(2, in );
            //输出流   从java程序中输出到数据库表中
            int count = ps.executeUpdate();
            System.out.println("插入了" + count + "条记录");
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            DbUtils.close(conn, ps, null);
        }
    }
}
